//
//  SN_QuestionReadMore_Picker.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/8/4.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_QuestionReadMore_Picker.h"
#import "SN_NewspaperModel.h"
#import "SN_AlertModel.h"
@interface SN_QuestionReadMore_Picker() <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) UIView *toolBar;

@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, assign) NSInteger selectYearIndex; //选择的年份
@property (nonatomic, assign) NSInteger selectPhaseIndex; //选择的期数

@end

@implementation SN_QuestionReadMore_Picker

#pragma mark -Getter
- (UIView *)toolBar {
    
    if (!_toolBar) {
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction)];
        [self addGestureRecognizer:tap];
        
        _toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.picker.y - 44, SCREENWIDTH, 44)];
        _toolBar.backgroundColor = [UIColor whiteColor];
        
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _cancelBtn.frame = CGRectMake(0, 0, 60, 44);
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [_toolBar addSubview:_cancelBtn];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, SCREENWIDTH - 120, 44)];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_toolBar addSubview:_titleLabel];
        
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _confirmBtn.frame = CGRectMake(SCREENWIDTH - 60, 0, 60, 44);
        [_toolBar addSubview:_confirmBtn];
        
        UIView *linv = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, SCREENWIDTH, 0.5)];
        linv.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [_toolBar addSubview:linv];
        
    }
    
    return _toolBar;
}

- (UIPickerView *)picker {
    
    if (!_picker) {
        
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREENHEIGH - 125, SCREENWIDTH, 125)];
        _picker.delegate = self;
        _picker.dataSource = self;
        _picker.backgroundColor = [UIColor whiteColor];
    
    }
    
    return _picker;
}

#pragma Setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    
    _dataArr = dataArr;
    SN_AlertModel *model = _dataArr[0];
//    SN_NewspaperModel *newsModel = model.phaselist[0];
    
    _titleLabel.text = [NSString stringWithFormat:@"%@%@%@",model.edtionntypeame,model.gradername,model.coursename];
    [self.picker reloadAllComponents];
}

- (instancetype)initWithSuperView:(UIView *)superView {
    
    if (self = [super init]) {
        
        [self initDatas];
        
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = RGBACOLOR(0, 0, 0, 0.2);
        [self addSubview:self.toolBar];
        [self addSubview:self.picker];
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    
    return self;
}

- (void)initDatas {
    
    _selectYearIndex = 0;
    _selectPhaseIndex = 0;
}

#pragma mark -UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        
        return self.dataArr.count;
        
    }else {
        
        SN_AlertModel *model = [self.dataArr objectAtIndex:_selectYearIndex];
        
        return model.phaselist.count;
    }
}

#pragma mark -UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        
        SN_AlertModel *model = [self.dataArr objectAtIndex:row];
        return [NSString stringWithFormat:@"%@学年度", model.year];
    }else {
        SN_AlertModel *model = [self.dataArr objectAtIndex:_selectYearIndex];
        
        SN_NewspaperModel *newsModel = [model.phaselist objectAtIndex:row];
        return [NSString stringWithFormat:@"第%ld期", (long)newsModel.phase];
    }
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        
        _selectYearIndex = row;
        [pickerView reloadComponent:1];
        
    }else {
        
        _selectPhaseIndex = row;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *label = (UILabel *)view;
    
    if (!label) {
        label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = YES;
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
    }
    
    label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return label;
}

#pragma mark -Action
- (void)cancelAction {

    [self removeFromSuperview];
}

- (void)confirmAction {

    SN_AlertModel *model = [self.dataArr objectAtIndex:_selectYearIndex];
    
    SN_NewspaperModel *newsModel = [model.phaselist objectAtIndex:_selectPhaseIndex];
    
    [self removeFromSuperview];
    
    if (_block) {
        
        _block(newsModel,model);
    }
}

@end
